<?php

namespace app\index\controller;

use app\index\model\Good as ModelGood;
use frame\lib\Db;
use frame\lib\VerfiyParam;

class Good extends Base
{
    public function index()
    {
        $this->view();
    }
    public function list()
    {
        $good = @$_GET['good'];
        if (!empty($good)) {
            $good = trim($good);
        }
        success(ModelGood::list($good));
    }
    public function edit()
    {
        $id = $_GET['id'];
        if(empty($id)) error('缺少参数id',201);
        $data = Db::name('good')->where(['id' => $id,'user_id' => $this->uid])->find();
        if(!$data) error('商品不存在');
        $this->view('edit' , ['data' => $data]);
    }
    public function doEdit() {
        $param = $_POST;
        if(!array_key_exists('id',$param)) error('缺少参数id' , 201);
        $good = Db::name('good')->where(['id' => $param['id'] , 'user_id' => $this->uid])->find();
        if(!$good) error('商品不存在');
        $rule = [
            'good_name' => 'require'
            ,'score' => 'require|int|gt:0'
            ,'type' => 'require|in:1,2,3,4'
            ,'stock' => 'require|gt:0'
        ];
        VerfiyParam::init($param , $rule);
        $vip_time = vipTimeCompute(@$param['vip_type'], @$param['vip_count']);
        $data = [
            'good_name' => $param['good_name'] ?? $good['good_name'],
            'stock' => $param['stock'] ?? $good['stock'],
            'type' => $param['type'] ?? $good['type'],
            'vip_time' => @$vip_time ?? $good['vip_time'],
            'fake_type' => @$param['fake_type'] ?? $good['fake_type'],
            'vip_type' => @$param['vip_type'] ?? $good['vip_type'],
            'vip_count' => @$param['vip_count'] ?? $good['vip_count'],
            'status' => isset($param['status']) ? 1 : 0,
            'user_id' => $this->uid,
            'update_time' => getTime()
        ];
        $res = Db::name('good')->where(['user_id' => $this->uid , 'id' => $param['id']])->update($data);
        if($res) success();
        error();
    }
    public function del()
    {
        $id = @$_GET['id'];
        if (!$id) error('缺少参数ID', 201);
        $id = explode(',', $id);
        $res = Db::name('good')->where(['user_id' => $this->uid])->whereIn('id', $id)->delete();
        if ($res) success();
        error();
    }
    public function add()
    {
        $this->view('add');
    }
    public function doAdd()
    {
        $param = $_POST;
        $rule = [
            'good_name' => 'require'
            ,'score' => 'require|int|gt:0'
            ,'type' => 'require|in:1,2,3,4'
            ,'stock' => 'require|gt:0'
        ];
        VerfiyParam::init($param , $rule);
        if ($param['type'] == 2) {
            if (!array_key_exists('vip_count', $param)) error('缺少参数vip_count', 201);
            if (!array_key_exists('vip_type', $param)) error('缺少参数vip_type', 201);
        }else if($param['type'] == 3){
            if (!array_key_exists('fake_type', $param)) error('缺少参数fake_type', 201);
        }
        $vip_time = vipTimeCompute(@$param['vip_type'], @$param['vip_count']);
        $data = [
            'good_name' => $param['good_name'],
            'stock' => $param['stock'],
            'score' => $param['score'],
            'type' => $param['type'],
            'vip_time' => @$vip_time ?? 0,
            'fake_type' => @$param['fake_type'] ?? 1,
            'vip_type' => @$param['vip_type'] ?? 1,
            'vip_count' => @$param['vip_count'] ?? 1,
            'status' => isset($param['status']) ? 1 : 0,
            'user_id' => $this->uid,
            'create_time' => getTime()
        ];
        $count = Db::name('good')->where(['user_id' => $this->uid])->count();
        if($count > 50) error('商品库存最大为 <50>');
        $res = Db::name('good')->insert($data);
        if ($res) success();
        error();
    }
}
